package com.autohammal.ui.frame;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CTabFolder;
import org.eclipse.swt.custom.CTabItem;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.ExpandBar;
import org.eclipse.swt.widgets.ExpandItem;
import org.eclipse.wb.swt.SWTResourceManager;

import swing2swt.layout.BorderLayout;

import com.autohammal.ITarget;
import com.autohammal.exception.AutoHammalRuntimeException;
import com.autohammal.implement.FileCheckTarget;
import com.autohammal.implement.RunApplicationTarget;
import com.autohammal.ui.frame.data.ItemsCompositeViewer;
import com.autohammal.ui.frame.data.ItemsCompositeViewer.ItemView;
import com.autohammal.ui.frame.data.ItemsCompositeViewerContainer;
import com.autohammal.ui.frame.data.TargetForComboxViewer;
import com.autohammal.ui.frame.data.ViewData;
import com.autohammal.ui.frame.data.ViewState;
import com.autohammal.ui.frame.data.ViewerContainer;
import com.autohammal.ui.frame.items.ItemDescriptor;

public class HammalConfigComposite2 extends Composite {

	/**
	 * Create the composite.
	 * 
	 * @param parent
	 * @param style
	 */
	public HammalConfigComposite2(Composite parent, int style) {
		super(parent, SWT.NONE);
		setBackground(SWTResourceManager
				.getColor(SWT.COLOR_TITLE_INACTIVE_FOREGROUND));
		setLayout(new FillLayout(SWT.HORIZONTAL));

		ExpandBar expandBar = new ExpandBar(this, SWT.NONE);
		expandBar.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));

		ExpandItem xpndtmTarget = new ExpandItem(expandBar, SWT.NONE);
		xpndtmTarget.setExpanded(true);
		xpndtmTarget.setText("Target");

		Composite composite = new Composite(expandBar, SWT.NONE);
		composite.setBackground(SWTResourceManager
				.getColor(SWT.COLOR_TITLE_INACTIVE_FOREGROUND));
		xpndtmTarget.setControl(composite);
		composite.setLayout(new BorderLayout(0, 0));

		Composite composite_3 = new Composite(composite, SWT.NONE);
		composite_3.setLayoutData(BorderLayout.NORTH);
		composite_3.setLayout(new FillLayout(SWT.HORIZONTAL));

		comboTarget = new Combo(composite_3, SWT.READ_ONLY);
		comboTarget.setData(viewers);
		comboTarget.addSelectionListener(new SelectionAdapter() {
			@SuppressWarnings("unchecked")
			@Override
			public void widgetSelected(SelectionEvent e) {
				ViewData viewData = viewers
						.get(comboTarget.getSelectionIndex()).getOriginView();
				loadItems((Class<? extends ITarget>) viewData.getData());
			}
		});

		tabConfigFolder = new CTabFolder(composite, SWT.BORDER);
		tabConfigFolder.setLayoutData(BorderLayout.CENTER);
		tabConfigFolder.setSelectionBackground(Display.getCurrent()
				.getSystemColor(SWT.COLOR_TITLE_INACTIVE_BACKGROUND_GRADIENT));

		tbtmConfig = new CTabItem(tabConfigFolder, SWT.NONE);
		tbtmConfig.setText("Configuration Items");

		tbtmInput = new CTabItem(tabConfigFolder, SWT.NONE);
		tbtmInput.setText("Input Items");

		tbtmOutput = new CTabItem(tabConfigFolder, SWT.NONE);
		tbtmOutput.setText("Output Items");
		xpndtmTarget.setHeight(120);

		ExpandItem xpndtmScheduler = new ExpandItem(expandBar, SWT.NONE);
		xpndtmScheduler.setExpanded(true);
		xpndtmScheduler.setText("Scheduler");

		Composite composite_1 = new Composite(expandBar, SWT.NONE);
		composite_1.setBackground(SWTResourceManager
				.getColor(SWT.COLOR_TITLE_INACTIVE_FOREGROUND));
		xpndtmScheduler.setControl(composite_1);
		composite_1.setLayout(new FillLayout(SWT.HORIZONTAL));

		CTabFolder tabSchedulerFolder = new CTabFolder(composite_1, SWT.BORDER);
		tabSchedulerFolder.setSelectionBackground(Display.getCurrent()
				.getSystemColor(SWT.COLOR_TITLE_INACTIVE_BACKGROUND_GRADIENT));

		CTabItem tbtmNewItem_3 = new CTabItem(tabSchedulerFolder, SWT.NONE);
		tbtmNewItem_3.setText("Simple");

		CTabItem tbtmNewItem_4 = new CTabItem(tabSchedulerFolder, SWT.NONE);
		tbtmNewItem_4.setText("Advance");

		CTabItem tbtmNewItem_5 = new CTabItem(tabSchedulerFolder, SWT.NONE);
		tbtmNewItem_5.setText("WorkDays");
		xpndtmScheduler.setHeight(80);

		ExpandItem xpndtmOptions = new ExpandItem(expandBar, SWT.NONE);
		xpndtmOptions.setExpanded(true);
		xpndtmOptions.setText("Options");

		Composite composite_2 = new Composite(expandBar, SWT.NONE);
		composite_2.setBackground(SWTResourceManager
				.getColor(SWT.COLOR_TITLE_INACTIVE_FOREGROUND));
		xpndtmOptions.setControl(composite_2);
		xpndtmOptions.setHeight(xpndtmOptions.getControl().computeSize(
				SWT.DEFAULT, SWT.DEFAULT).y);

		initializeItems();
	}

	ViewerContainer<TargetForComboxViewer> viewers = new ViewerContainer<TargetForComboxViewer>();
	private CTabFolder tabConfigFolder;
	private CTabItem tbtmConfig;
	private CTabItem tbtmInput;
	private CTabItem tbtmOutput;
	private Combo comboTarget;

	protected void initializeItems() {
		TargetForComboxViewer viewer1 = viewers
				.newViewer(TargetForComboxViewer.class);
		viewer1.newViewData("TARGET_ORIGIN", FileCheckTarget.class,
				ViewState.ORIGIN);
		viewer1.newViewData("TARGET_VIEW_0", "FileCheckTarget",
				ViewState.MASTER);
		// RunApplicationTarget
		TargetForComboxViewer viewer2 = viewers
				.newViewer(TargetForComboxViewer.class);
		viewer2.newViewData("TARGET_ORIGIN", RunApplicationTarget.class,
				ViewState.ORIGIN);
		viewer2.newViewData("TARGET_VIEW_0", "RunApplicationTarget",
				ViewState.MASTER);
		comboTarget.setItems(new String[] { viewer1.getMasterViewValue(),
				viewer2.getMasterViewValue() });
	}

	private ItemsCompositeViewerContainer itemsViewerContainer = new ItemsCompositeViewerContainer();

	protected void loadItems(Class<? extends ITarget> clz) {
		try {
			ItemsCompositeViewer viewer = itemsViewerContainer
					.newItemsCompositeViewer(clz);
			ITarget target = null;
			if (viewer.getItemViewValue(ItemView.CONFIG) == null) {
				target = clz.newInstance();
				viewer.setItemsView(ItemView.CONFIG,
						makeScrolledConfigComposite(target
								.configurationDescriptors()));
			}
			if (viewer.getItemViewValue(ItemView.INPUT) == null) {
				viewer.setItemsView(ItemView.INPUT,
						makeScrolledConfigComposite(target.inputDescriptors()));
			}
			if (viewer.getItemViewValue(ItemView.OUTPUT) == null) {
				viewer.setItemsView(ItemView.OUTPUT,
						makeScrolledConfigComposite(target.outputDescriptors()));
			}
			tbtmConfig.setControl(viewer.getItemViewValue(ItemView.CONFIG));
			tbtmInput.setControl(viewer.getItemViewValue(ItemView.INPUT));
			tbtmOutput.setControl(viewer.getItemViewValue(ItemView.OUTPUT));
			tabConfigFolder.setSelection(tbtmConfig);
		} catch (Exception e) {
			throw new AutoHammalRuntimeException(e);
		}
	}

	private ScrolledComposite makeScrolledConfigComposite(
			ItemDescriptor[] itemDess) {
		ScrolledComposite scrolled = new ScrolledComposite(tabConfigFolder,
				SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
		scrolled.setExpandHorizontal(true);
		scrolled.setExpandVertical(true);
		Composite content = new ItemsConfigComposite(itemDess, scrolled,
				SWT.NONE);
		scrolled.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1,
				1));
		scrolled.setContent(content);
		scrolled.setMinSize(content.computeSize(SWT.DEFAULT, SWT.DEFAULT));
		scrolled.setShowFocusedControl(true);
		return scrolled;
	}

	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}
}
